package com.egao.orderB.biz.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;

import com.egao.common.core.damon.utils.Utils;
import com.egao.common.system.entity.User;
import com.egao.orderB.biz.WarehouseBiz;
import com.egao.orderB.entity.Warehouse;
import com.egao.common.core.web.JsonResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2021-12-23 16:29:28
 * @version ：1.0
 */
@Repository
public class WarehouseBizImpl extends BaseDao implements WarehouseBiz {

	@Override
	public JsonResult list(PageResult2<T> page, Warehouse warehouse, User loginUser) throws Exception {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Warehouse> rm = new BeanPropertyRowMapper<Warehouse>(Warehouse.class);
		StringBuffer sql = new StringBuffer("select w.id,w.name,w.remark");
    sql.append(",w.order_num,w.create_time,w.create_user,w.update_time");
		sql.append(",w.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.nick_name createName ");
    sql.append(" from chf_warehouse w ");
    sql.append(" inner join sys_user suser on suser.user_id=w.create_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=w.organization_id and sogzn.deleted=0 ");
    sql.append(" where w.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
      sql.append(" and w.organization_id=:tableOrganizationId ");
      map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
    }
    if(Utils.isNotEmpty(warehouse)){
//			if(Utils.isNotEmpty(warehouse.getName())){
//				sql.append(" and instr(w.name,:name)>0 ");
//				map.put("name", warehouse.getName());
//			}
      if(Utils.isNotEmpty(warehouse.getSort())&&Utils.isNotEmpty(warehouse.getOrder())){
        sql.append(" order by w."+warehouse.getSort()+" "+warehouse.getOrder());
      } else {
        sql.append(" order by w.create_time desc");
      }
		}
		return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<Warehouse> list(Warehouse warehouse, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Warehouse> rm = new BeanPropertyRowMapper<Warehouse>(Warehouse.class);
    StringBuffer sql = new StringBuffer("select w.id,w.name,w.remark");
    sql.append(",w.order_num,w.create_time,w.create_user,w.update_time");
		sql.append(",w.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.nick_name createName ");
    sql.append(" from chf_warehouse w ");
    sql.append(" inner join sys_user suser on suser.user_id=w.create_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=w.organization_id and sogzn.deleted=0 ");
    sql.append(" where w.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
      sql.append(" and w.organization_id=:tableOrganizationId ");
      map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
    }
    if(Utils.isNotEmpty(warehouse)){
      if(Utils.isNotEmpty(warehouse.getSort())&&Utils.isNotEmpty(warehouse.getOrder())){
        sql.append(" order by w."+warehouse.getSort()+" "+warehouse.getOrder());
      } else {
        sql.append(" order by w.create_time desc");
      }
    }
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public Warehouse getById(Warehouse warehouse) {
		RowMapper<Warehouse> rm = new BeanPropertyRowMapper<Warehouse>(Warehouse.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select w.id,w.name,w.remark");
    sql.append(",w.order_num,w.create_time,w.create_user,w.update_time");
		sql.append(",w.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.nick_name createName ");
    sql.append(" from chf_warehouse w ");
    sql.append(" inner join sys_user suser on suser.user_id=w.create_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=w.organization_id and sogzn.deleted=0 ");
    sql.append(" where w.deleted=0 and w.id=:id ");
		map.put("id", warehouse.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public boolean save(Warehouse warehouse, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into chf_warehouse (");
    sql.append("name,remark,order_num,create_user,create_time) values ( ");
    sql.append(":name,:remark,:order_num,:create_user,now())");
    map.put("name", warehouse.getName());
    map.put("remark", warehouse.getRemark());
    map.put("order_num", warehouse.getOrderNum()==null?null:warehouse.getOrderNum()+"");
		map.put("create_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(Warehouse warehouse, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_warehouse set ");
    sql.append("name=:name,remark=:remark,order_num=:order_num ");
		sql.append(",update_time=now(),update_user=:update_user where id=:id ");
    map.put("name", warehouse.getName());
    map.put("remark", warehouse.getRemark());
    map.put("order_num", warehouse.getOrderNum()==null?null:warehouse.getOrderNum()+"");
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", warehouse.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean remove(Warehouse warehouse, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(warehouse.getId().length()>0){
			for(String ID : warehouse.getId().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update chf_warehouse set deleted=1" +
						",update_user=:update_user, update_time=now() "
						+ " where id =:id ");
				map.put("update_user", loginUser.getUserId() + "");
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
